package temp;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class StringUtil {
    private static final String RANDOM_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static Random rand = new Random();

    /**
     * 获得一个随机的字符串
     * 
     * @param len
     * @return
     */
    public static String getRandomString(int len) {
        char[] buf = new char[len];
        for (int i = 0; i < len; i++) {
            buf[i] = RANDOM_CHARS.charAt(rand.nextInt(RANDOM_CHARS.length()));
        }
        return new String(buf);
    }

    /**
     * 首字母大写
     * 
     * @param str
     * @return
     */
    public static String upCaseFirst(String str) {
        char[] arr = str.toCharArray();
        arr[0] = Character.toUpperCase(arr[0]);
        return new String(arr);
    }

    public static String trim(String str) {
        if (str == null) {
            return "";
        }
        return str.trim();
    }

    /**
     * 检查空字符串，或者" "字符串
     * 
     * @param str
     * @return
     */
    public static boolean isBlank(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if ((Character.isWhitespace(str.charAt(i)) == false)) {
                return false;
            }
        }
        return true;
    }

    public static boolean isNotBlank(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return false;
        }
        for (int i = 0; i < strLen; i++) {
            if ((Character.isWhitespace(str.charAt(i)) == false)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 从s1中截取s2和s3中间的字符串,找不到返回""
     * 
     * @param s1
     *            源字符串
     * @param s2
     *            开始字符串
     * @param s3
     *            结束字符串
     * @return
     */
    public static String getBetweenString(String s1, String s2, String s3) {
        char[] arr1 = s1.toCharArray();
        char[] arr2 = s2.toCharArray();
        char[] arr3 = s3.toCharArray();
        boolean isFoundHead = false;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < arr1.length; i++) {
            if (!isFoundHead && arr1[i] == arr2[0] && isMatch(arr1, i, arr2)) {// 找到开头
                i += arr2.length;
                isFoundHead = true;
            }
            if (isFoundHead) {
                if (arr1[i] == arr3[0] && isMatch(arr1, i, arr3)) {// 找到结尾
                    return sb.toString().trim();
                } else {
                    sb.append(arr1[i]);
                }
            }
        }
        return "";
    }

    // 从arr1的start匹配arr2
    public static boolean isMatch(char[] arr1, int start, char[] arr2) {
        int l2 = arr2.length;
        int l1 = arr1.length;
        if (l2 + start > l1) {
            return false;
        }
        for (int j = 0; j < l2; j++) {
            if (arr1[start + j] != arr2[j]) {
                return false;
            }
        }
        return true;
    }


    public static String getFileType(String fileName) {
        if (fileName == null) {
            return null;
        }
        int index = fileName.lastIndexOf(".");
        if (index < 0) {
            return null;
        }
        return fileName.substring(index);
    }

    public static List<String> strToList(String str, String regex) {
        String[] arr = StringUtil.trim(str).split(regex);
        List<String> result = new ArrayList<>(arr.length);
        for (String s : arr) {
            if (StringUtil.isBlank(s)) {
                continue;
            }
            result.add(StringUtil.trim(s));
        }
        return result;
    }


    /**
     * 
     * @param format
     *            把{}替换为args的参数
     * @param args
     * @return
     */
    public static String format(String format, Object... args) {
        if (format == null) {
            return "";
        }
        if (args == null || args.length == 0 || format.length() < 2) {
            return format;
        }
        char[] arr = format.toCharArray();
        StringBuilder sb = new StringBuilder(arr.length);
        int argIndex = 0, argLength = args.length;
        int i = 0, length = arr.length - 1;
        for (; i < length; i++) {
            char c = arr[i];
            if (c == '{' && argIndex < argLength && arr[i + 1] == '}') {
                i++;
                sb.append(args[argIndex++]);
                continue;
            }
            sb.append(c);
        }
        char c = arr[length];
        if (c != '}') {
            sb.append(c);
        } else {
            length = sb.length();
            if (length > 0 && sb.charAt(length - 1) == '{') {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    public static boolean startWith(String sourceString, String startString) {
        int length = startString.length();
        if (length == 0) {
            return true;
        }
        if (sourceString.length() < length) {
            return false;
        }
        char[] sourceArr = sourceString.toCharArray();
        char[] startArr = startString.toCharArray();
        for (int i = 0, len = startArr.length; i < len; i++) {
            if (sourceArr[i] != startArr[i]) {
                return false;
            }
        }
        return true;
    }

    public static boolean endWith(String sourceString, String endString) {
        int length = endString.length();
        if (length == 0) {
            return true;
        }
        if (sourceString.length() < length) {
            return false;
        }
        char[] sourceArr = sourceString.toCharArray();
        char[] endArr = endString.toCharArray();
        int sourceIndex = sourceArr.length - 1;
        int endIndex = endArr.length - 1;
        while (endIndex >= 0) {
            if (sourceArr[sourceIndex] != endArr[endIndex]) {
                return false;
            }
            endIndex--;
            sourceIndex--;
        }
        return true;
    }
}
